rm(list = ls())

library(checkpoint)
library(texreg)


resultspath <- "path_to_estimates" # from models_appendix_c.R
wd <- "your_own_wd"

load(file=paste0(resultspath, 'm_1.RData'))
load(file=paste0(resultspath, 'm_2.RData'))
load(file=paste0(resultspath, 'm_3.RData'))
load(file=paste0(resultspath, 'm_4.RData'))
load(file=paste0(resultspath, 'm_5.RData'))
load(file=paste0(resultspath, 'm_6.RData'))

###########
# Table 1 #
###########

htmlreg(l=list(m.1[[1]], m.1[[2]], m.2[[1]]),
        digits=2, file=paste0(wd, "table1.html"),
        caption="",
        single.row=TRUE,
        custom.gof.names = c(NA,
                             NA,
                             NA,
                             NA,
                             "Country*Years",
                             "Countries",
                             "Var Country*Years: Intercept",
                             "Var Country*Years: Extreme Left",
                             "Var Country*Years: Center",
                             "Var Country*Years: Right",
                             "Var Country*Years: Extreme Right",
                             "Cov Country*Years: Intercept, Ext Left",
                             "Cov Country*Years: Intercept, Center",
                             "Cov Country*Years: Intercept, Right",
                             "Cov Country*Years: Intercept, Ext Right",
                             "Cov Country*Years: Ext Left, Center",
                             "Cov Country*Years: Ext Left, Right",
                             "Cov Country*Years: Ext Left, Ext Right",
                             "Cov Country*Years: Center, Right",
                             "Cov Country*Years: Center, Ext Right",
                             "Cov Country*Years: Right, Ext Right",
                             "Var Country: Intercept"),
        custom.coef.map = list("(Intercept)"="Intercept",
                               rideol1="Extreme left (base: left)",
                               rideol3="Center",
                               rideol4="Right",
                               rideol5="Extreme right",
                               gov_dist="Distance from the gov.",
                               union1 = "Union member (dichot.)",
                               prtdgcl_num = "Party ID (cont.)",
                               gincdif = "Income diff. (cont.)",
                               freehms = "Gay rights (cont.)",
                               conformity = "Conformity (cont.)",
                               universalism = "Universalism (cont.)",
                               security = "Security (cont.)",
                               gender1="Male",
                               agea="Age (cont.)",
                               eduyrs="Years of education (cont.)",
                               domicil2="Suburbs (base: big city)",
                               domicil3="Small City",
                               domicil4="Village",
                               domicil5="Countryside",
                               unemployed1="Unemployed",
                               transition="Transition protest level",
                               gov_genlr="Gov. L-R",
                               gdp_wb_ppp="GDP (ppp, 2011 US$)",
                               wb_vae="Voice & Accountability",
                               rsquares_std="Adj R sq. of soc. structure for L-R",
                               ri_l2_std="Rel.imp. of parties for L-R",
                               mean_univ="Avg. universalism",
                               mean_conf="Avg. conformity",
                               mean_secur="Avg. security",
                               region2="Southern Europe (base: Northwestern)",
                               region3="Eastern Europe",
                               year_num="Year of Survey",
                               "region2:rideol1"="Ext Left*SE",
                               "region2:rideol2"="Left*SE",
                               "region2:rideol3"="Center*SE",
                               "region2:rideol4"="Right*SE",
                               "region2:rideol5"="Ext Right*SE",
                               "region3:rideol1"="Ext Left*EE",
                               "region3:rideol2"="Left*EE",
                               "region3:rideol3"="Center*EE",
                               "region3:rideol4"="Right*EE",
                               "region3:rideol5"="Ext Right*EE",
                               "rideol1:year_num"="Ext Left*Year",
                               "rideol3:year_num"="Center*Year",
                               "rideol4:year_num"="Right*Year",
                               "rideol5:year_num"="Ext Right*Year",
                               "region2:rideol1:year_num"="SE*Year*Ext Left",
                               "region3:rideol1:year_num"="EE*Year*Ext Left",
                               "region2:rideol3:year_num"="SE*Year*Center",
                               "region3:rideol3:year_num"="EE*Year*Center",
                               "region2:rideol4:year_num"="SE*Year*Right",
                               "region3:rideol4:year_num"="EE*Year*Right",
                               "region2:rideol5:year_num"="SE*Year*Ext Right",
                               "region3:rideol5:year_num"="EE*Year*Ext Right"))


###############################
# Table 2 - regression models #
###############################

htmlreg(l=list(m.3[[1]], m.3[[2]], m.4[[1]]),
        digits=2, file=paste0(wd, "table2_app.html"),
        single.row=TRUE,
        caption="",
        custom.gof.names = c(NA,
                             NA,
                             NA,
                             NA,
                             "Country*Years",
                             "Countries",
                             "Var Country*Years: Intercept",
                             "Var Country*Years: Extreme Left",
                             "Var Country*Years: Center",
                             "Var Country*Years: Right",
                             "Var Country*Years: Extreme Right",
                             "Cov Country*Years: Intercept, Ext Left",
                             "Cov Country*Years: Intercept, Center",
                             "Cov Country*Years: Intercept, Right",
                             "Cov Country*Years: Intercept, Ext Right",
                             "Cov Country*Years: Ext Left, Center",
                             "Cov Country*Years: Ext Left, Right",
                             "Cov Country*Years: Ext Left, Ext Right",
                             "Cov Country*Years: Center, Right",
                             "Cov Country*Years: Center, Ext Right",
                             "Cov Country*Years: Right, Ext Right",
                             "Var Country: Intercept"),
        custom.coef.map = list("(Intercept)"="Intercept",
                               soc_pop_eleches="Exposure",
                               rideol1="Extreme left (base: left)",
                               rideol3="Center",
                               rideol4="Right",
                               rideol5="Extreme right",
                               gov_dist="Distance from the gov.",
                               union1 = "Union member (dichot.)",
                               prtdgcl_num = "Party ID (cont.)",
                               gincdif = "Income diff. (cont.)",
                               freehms = "Gay rights (cont.)",
                               conformity = "Conformity (cont.)",
                               universalism = "Universalism (cont.)",
                               security = "Security (cont.)",
                               gender1="Male",
                               agea="Age (cont.)",
                               eduyrs="Years of education (cont.)",
                               domicil2="Suburbs (base: big city)",
                               domicil3="Small City",
                               domicil4="Village",
                               domicil5="Countryside",
                               unemployed1="Unemployed",
                               transition="Transition protest level",
                               gov_genlr="Gov. L-R",
                               gdp_wb_ppp="GDP (ppp, 2011 US$)",
                               wb_vae="Voice & Accountability",
                               rsquares_std="Adj R sq. of soc. structure for L-R",
                               ri_l2_std="Rel.imp. of parties for L-R",
                               mean_univ="Avg. universalism",
                               mean_conf="Avg. conformity",
                               mean_secur="Avg. security",
                               region3="Eastern Europe (base: Southern Europe)",
                               "soc_pop_eleches:region3"="Exposure*EE",
                               "region3:soc_pop_eleches"="Exposure*EE",
                               "region3:rideol1"="Ext Left*EE",
                               "region3:rideol2"="Left*EE",
                               "region3:rideol3"="Center*EE",
                               "region3:rideol4"="Right*EE",
                               "region3:rideol5"="Ext Right*EE",
                               "rideol1:soc_pop_eleches"="Ext Left*Exposure",
                               "rideol2:soc_pop_eleches"="Left*Exposure",
                               "rideol3:soc_pop_eleches"="Center*Exposure",
                               "rideol4:soc_pop_eleches"="Right*Exposure",
                               "rideol5:soc_pop_eleches"="Ext Right*Exposure",
                               "region3:rideol1:soc_pop_eleches"="EE*Ext Left*Exposure",
                               "region3:rideol2:soc_pop_eleches"="EE*Left*Exposure",
                               "region3:rideol3:soc_pop_eleches"="EE*Center*Exposure",
                               "region3:rideol4:soc_pop_eleches"="EE*Right*Exposure",
                               "region3:rideol5:soc_pop_eleches"="EE*Ext Right*Exposure"))

#################
#### Table 3 ####
#################

htmlreg(l=list(m.4[[2]], m.5[[1]]),
        caption = "",  
        digits=2, file=paste0(wd, "table3_app.html"),
        single.row=TRUE,
        custom.gof.names = c(NA,
                             NA,
                             NA,
                             NA,
                             "Country*Years",
                             "Countries",
                             "Var Country*Years: Intercept",
                             "Var Country*Years: Extreme Left",
                             "Var Country*Years: Center",
                             "Var Country*Years: Right",
                             "Var Country*Years: Extreme Right",
                             "Cov Country*Years: Intercept, Ext Left",
                             "Cov Country*Years: Intercept, Center",
                             "Cov Country*Years: Intercept, Right",
                             "Cov Country*Years: Intercept, Ext Right",
                             "Cov Country*Years: Ext Left, Center",
                             "Cov Country*Years: Ext Left, Right",
                             "Cov Country*Years: Ext Left, Ext Right",
                             "Cov Country*Years: Center, Right",
                             "Cov Country*Years: Center, Ext Right",
                             "Cov Country*Years: Right, Ext Right",
                             "Var Country: Intercept"),
        custom.coef.map = list("(Intercept)"="Intercept",
                               rideol1="Extreme left (base: left)",
                               rideol3="Center",
                               rideol4="Right",
                               rideol5="Extreme right",
                               gov_dist="Distance from the gov.",
                               union1 = "Union member (dichot.)",
                               prtdgcl_num = "Party ID (cont.)",
                               gincdif = "Income diff. (cont.)",
                               freehms = "Gay rights (cont.)",
                               conformity = "Conformity (cont.)",
                               universalism = "Universalism (cont.)",
                               security = "Security (cont.)",
                               gender1="Male",
                               agea="Age (cont.)",
                               eduyrs="Years of education (cont.)",
                               domicil2="Suburbs (base: big city)",
                               domicil3="Small City",
                               domicil4="Village",
                               domicil5="Countryside",
                               unemployed1="Unemployed",
                               transition="Transition protest level",
                               gov_genlr="Gov. L-R",
                               gdp_wb_ppp="GDP (ppp, 2011 US$)",
                               wb_vae="Voice & Accountability",
                               rsquares_std="Adj R sq. of soc. structure for L-R",
                               ri_l2_std="Rel.imp. of parties for L-R",
                               mean_univ="Avg. universalism",
                               mean_conf="Avg. conformity",
                               mean_secur="Avg. security",
                               region2="Southern Europe (base: Northwestern)",
                               region3="Eastern Europe",
                               "region2:prtdgcl_num"="SE:Party ID",
                               "region3:prtdgcl_num"="EE:Party ID",
                               "region2:rideol1"="SE:Ext Left",
                               "region3:rideol1"="EE:Ext Left",
                               "region2:rideol3"="SE:Center",
                               "region3:rideol3"="EE:Center",
                               "region2:rideol4"="SE:Right",
                               "region3:rideol4"="EE:Right",
                               "region2:rideol5"="SE:Ext Right",
                               "region3:rideol5"="EE:Ext Right",
                               "rideol1:prtdgcl_num"="Ext Left:Party ID",
                               "rideol3:prtdgcl_num"="Center:Party ID",
                               "rideol4:prtdgcl_num"="Right:Party ID",
                               "rideol5:prtdgcl_num"="Ext Right:Party ID",
                               "region2:rideol1:prtdgcl_num"="SE:Ext Left:Party ID",
                               "region3:rideol1:prtdgcl_num"="EE:Ext Left:Party ID",
                               "region2:rideol3:prtdgcl_num"="SE:Center:Party ID",
                               "region3:rideol3:prtdgcl_num"="EE:Center:Party ID",
                               "region2:rideol4:prtdgcl_num"="SE:Right:Party ID",
                               "region3:rideol4:prtdgcl_num"="EE:Right:Party ID",
                               "region2:rideol5:prtdgcl_num"="SE:Ext Right:Party ID",
                               "region3:rideol5:prtdgcl_num"="EE:Ext Right:Party ID"))

#################
#### Table 4 ####
#################

htmlreg(l=list(m.5[[2]], m.6[[1]], m.6[[2]]),
        caption = "",  
        digits=2, file=paste0(wd, "table4_app.html"),
        single.row=TRUE,
        custom.gof.names = c(NA,
                             NA,
                             NA,
                             NA,
                             "Country*Years",
                             "Countries",
                             "Var Country*Years: Intercept",
                             "Var Country*Years: Extreme Left",
                             "Var Country*Years: Center",
                             "Var Country*Years: Right",
                             "Var Country*Years: Extreme Right",
                             "Cov Country*Years: Intercept, Ext Left",
                             "Cov Country*Years: Intercept, Center",
                             "Cov Country*Years: Intercept, Right",
                             "Cov Country*Years: Intercept, Ext Right",
                             "Cov Country*Years: Ext Left, Center",
                             "Cov Country*Years: Ext Left, Right",
                             "Cov Country*Years: Ext Left, Ext Right",
                             "Cov Country*Years: Center, Right",
                             "Cov Country*Years: Center, Ext Right",
                             "Cov Country*Years: Right, Ext Right",
                             "Var Country: Intercept"),
        custom.coef.map = list("(Intercept)"="Intercept",
                               rideol1="Extreme left (base: left)",
                               rideol3="Center",
                               rideol4="Right",
                               rideol5="Extreme right",
                               gov_dist="Distance from the gov.",
                               union1 = "Union member (dichot.)",
                               prtdgcl_num = "Party ID (cont.)",
                               gincdif = "Income diff. (cont.)",
                               freehms = "Gay rights (cont.)",
                               conformity = "Conformity (cont.)",
                               universalism = "Universalism (cont.)",
                               security = "Security (cont.)",
                               gender1="Male",
                               agea="Age (cont.)",
                               eduyrs="Years of education (cont.)",
                               domicil2="Suburbs (base: big city)",
                               domicil3="Small City",
                               domicil4="Village",
                               domicil5="Countryside",
                               unemployed1="Unemployed",
                               transition="Transition protest level",
                               gov_genlr="Gov. L-R",
                               gdp_wb_ppp="GDP (ppp, 2011 US$)",
                               wb_vae="Voice & Accountability",
                               rsquares_std="Adj R sq. of soc. structure for L-R",
                               ri_l2_std="Rel.imp. of parties for L-R",
                               mean_univ="Avg. universalism",
                               mean_conf="Avg. conformity",
                               mean_secur="Avg. security",
                               region2="Southern Europe (base: Northwestern)",
                               region3="Eastern Europe",
                               "region2:gov_dist"="Gov. dist*SE",
                               "region3:gov_dist"="Gov. dist*EE",
                               "region2:gov_genlr"="Gov. L-R*SE",
                               "region3:gov_genlr"="Gov. L-R*EE",
                               "gov_genlr:gov_dist"="Gov. L-R*Gov. dist",
                               "region2:gov_genlr:gov_dist"="Gov. L-R*SE*Gov. dist",
                               "region3:gov_genlr:gov_dist"="Gov. L-R*EE*Gov. dist"))

